package com.zklh.leetcode.other;

import java.util.Arrays;

/**
 * 合并两个有序数组
 *
 * @Author tongnan
 * @Date 2024/4/11
 * @Version 1.0
 */
public class Solution88 {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        for (int i = m; i > 0; i--) {
            nums1[i+n-1] = nums1[i-1];
        }

        int index1 = n, index2 = 0, current = 0;
        while (current < nums1.length) {
            if (index1 < m + n && index2 < n) {
                if (nums1[index1] < nums2[index2]) {
                    nums1[current++] = nums1[index1++];
                } else {
                    nums1[current++] = nums2[index2++];
                }
            } else if (index1 == m + n) {
                nums1[current++] = nums2[index2++];
            } else if (index2 == n) {
                nums1[current++] = nums1[index1++];
            }
        }
    }

    public static void main(String[] args) {
        Solution88 solution88 = new Solution88();
        int[] nums1 = {0};
        int[] nums2 = {1};
        int m = nums1.length-nums2.length;
        int n = nums2.length;
        solution88.merge(nums1, m, nums2, n);
        System.out.println(Arrays.toString(nums1));
    }
}